#!/usr/bin/env python
"""
Preforms initial database population.
"""
import os
import sys
import random

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MabiCommerceWebProject.settings")
    from MabiCommerce.models import *
    
#************************************************************************************
#Begin editable section
#************************************************************************************

#Dictonary with server names as keys. Every value should be another dictonary, this time
#IP's as keys, and associated ports as tuples. HCH shouldn't be included....
    servers = {
        'Alexina' : {
            '8.31.99.87' : (11020, 11021, 11022),
            '8.31.99.90' : (11020, 11021, 11022, 11023)
        },
        'Mari': {
            '8.31.99.91' : (11020, 11021, 11022),
            '8.31.99.92' : (11020, 11021, 11022, 11023)
        },
        'Ruairi' : {
            '8.31.99.93' : (11020, 11021, 11022),
            '8.31.99.94' : (11020, 11021, 11022, 11023)        
        },
        'Tarlach' : {
            '8.31.99.95' : (11020, 11021, 11022),
            '8.31.99.96' : (11020, 11021, 11022, 11023)
        } 
    }
    
#Dictonary with town names as keys. Values are tuples of item names sold at that town    
    
    towns = {
        "Tir Chonaill" : ( "Baby Potion", "Diet Potion", "Snore Prevention Potion", "Wild Ginseng Potion", "Lovely Potion"),
        "Dunbarton"  : ( "Spider Gloves", "Wool Boots", "Ogre Executioner Mask", "Incubus Suit", "Succubus Swimsuit"),
        "Bangor"  : ( "Bangor Coal", "Marble", "Topaz", "Highlander Ore", "Lead"),
        "Emain Macha"  : ( "Berry Granola", "Butter Beer", "Smoked Wild Animal", "Triple Pasta", "Whole BBQ Bear"),
        "Taillteann"  : ( "Heat Crystal", "Music Box Preservation Stone", "Palala Crystal", "Circle Barrier Spike Crystal", "Alchemy Crystal"),
        "Tara"  : ( "Mini Dressing Table", "Tea Table", "Rocking Chair", "Bunk Bed", "Giant Wine Rack"),
        "Port Cobh"  : ( "Cobh Seaweed", "Cobh Oyster", "Shark Fin", "Jelly Fish", "Neid Scales"),
        "Belvast"  : ( "Iron Whip", "Dark Sword", "Safe", "Skeleton Ogre Armor", "Fake Morgant Helmet")
    }
    
    #However, dictonaries don't store the order of towns (nor should we rely on such a hack)
    #so this list holds the order that towns appear in any output.
    town_order = ["Tir Chonaill", "Dunbarton", "Bangor", "Emain Macha", "Taillteann", "Tara", "Port Cobh", "Belvast"]

#************************************************************************************
#End editable section
#************************************************************************************

    for server_name in servers:
        s, created = Server.objects.get_or_create(name=server_name)
        
        for ip, ports in servers[server_name].items():
            for port in ports:
                addr = IPaddress(IP = ip, port = port, server = s)
                addr.save()
            #end for
        #end for
        
        for town_name, item_names in towns.items():
            t, created = Town.objects.get_or_create(server = s, name = town_name, defaults={'position':town_order.index(town_name)})
            print t.name
            for num, ite in enumerate(item_names):
                print "\t%s :" % ite,
                for dest_town_name in towns:
                    if dest_town_name == town_name:
                        continue
                    #end if
                    print '%s,' % dest_town_name,
                    d_t, created = Town.objects.get_or_create(server = s, name = dest_town_name,defaults={'position':town_order.index(dest_town_name)} )
                    i = Item(name = ite, selling_town = t, destination_town = d_t, profit = 0, position=num)
                    i.save()
                #end for
                print ''
            #end for
            print "\r\n%s\r\n" % t
        #end for
    #end for